/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jsimplemarket.wicket.pages;

import com.googlecode.jsimplemarket.model.NTipTaxa;
import com.googlecode.jsimplemarket.model.Taxa;
import com.googlecode.jsimplemarket.service.GenericService;
import java.math.BigDecimal;
import java.util.List;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Alexandru.Codescu
 */
public class TaxaEditPage extends DefaultEditPage {
    @SpringBean(name = "genericService")
    private GenericService<Taxa> service;
    @SpringBean(name = "genericService")
    private GenericService<NTipTaxa> serviceTipTaxa;
    
    private final Taxa entity;
    
    public TaxaEditPage() {
        entity = new Taxa();
        init();
    }
    
    public TaxaEditPage(Long id) {
        entity = service.getById(Taxa.class, id);
        init();
    }
    
    private void init() {
        Form<Taxa> form = new Form<Taxa>("form");
        form.setModel(new CompoundPropertyModel<Taxa>(entity));
        
        TextField<String> tfCod = new TextField<String>("cod");
        tfCod.setRequired(true);
        form.add(tfCod);
        
        TextField<String> tfDenumire = new TextField<String>("denumire");
        tfDenumire.setRequired(true);
        form.add(tfDenumire);
        
        TextField<BigDecimal> tfDiscount = new TextField<BigDecimal>("discount");
        form.add(tfDiscount);
        
        TextField<BigDecimal> tfValoare = new TextField<BigDecimal>("valoare");
        tfValoare.setRequired(true);
        form.add(tfValoare);
        
        DropDownChoice<NTipTaxa> ddcTipTaxa = new DropDownChoice<NTipTaxa>("tipTaxa", new LoadableDetachableModel<List<NTipTaxa>>() {

            @Override
            protected List<NTipTaxa> load() {
                return serviceTipTaxa.getList(NTipTaxa.class, null, null);
            }
            
        }, new ChoiceRenderer<NTipTaxa>("denumire", "id"));
        ddcTipTaxa.setRequired(true);
        form.add(ddcTipTaxa);
        
        DateTextField dtfDataInceput = new DateTextField("dataInceput");
        dtfDataInceput.add(new DatePicker());
        form.add(dtfDataInceput);
        
        DateTextField dtfDataSfarsit = new DateTextField("dataSfarsit");
        dtfDataSfarsit.add(new DatePicker());
        form.add(dtfDataSfarsit);
        
        form.add(new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (entity.getId() == null) {
                    service.insert(entity);
                } else {
                    service.update(entity);
                }
                setResponsePage(new NomenclatorViewPage(Taxa.class, TaxaEditPage.class));
            }
            
        });
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(new NomenclatorViewPage(Taxa.class, TaxaEditPage.class));
            }
            
        });
        
        add(form);
    }
}
